package com.feicheng.controller.shop.wx.app.client;


import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.controller.BaseController;
import com.feicheng.persistence.entity.Account;
import com.feicheng.persistence.entity.Client;
import com.feicheng.service.IFClientService;
import com.feicheng.service.IFPayService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/wx/app/client/deposit-back-request")
@RequestMapping("/wx/app/client/deposit-back-request")
public class DepositBackRequestController extends BaseController {

    @Autowired
    IFClientService fClientService;
    
    @Resource(name="pay-service")
    Map<String, IFPayService> payServiceMap;

    @GetMapping("/info")
    public ResultInfo getBackInfo(Integer backId) {
        return fClientService.getDepositBackRequest(backId);
    }
    
    @GetMapping("/list")
    public ResultInfo getBackList(Integer depositId) {
    	JSONObject param = new JSONObject();
    	param.put(Constant.ENTITY.DEPOSIT_RECORD.DEPOSIT_ID, depositId);
    	return fClientService.getDepositBackRequestAll(param);
    }

    @PostMapping("/info")
    public ResultInfo newBackInfo(String reason, Integer depositId) {
        JSONObject param = new JSONObject();

        JSONObject current = super.getCurrentToken().getToken().getJSONObject(Constant.RESULT.CURRENT);
        Client currentClient = current.getObject(Constant.ENTITY.CLIENT.TABLE, Client.class);

        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.CLIENT_ID, currentClient.getClientId());
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.REASON, reason);
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.DEPOSIT_ID, depositId);

        ResultInfo result = fClientService.newDepositBackRequest(param);


        return result;
    }


    @PostMapping("/notice-url/{storeId}")
    public JSONObject refundNotice(
    		@RequestHeader("Wechatpay-Nonce") String nonce,
    		@RequestHeader("Wechatpay-Signature") String signature,
    		@RequestHeader("Wechatpay-Signature-Type") String signatureType,
    		@RequestHeader("Wechatpay-Timestamp") String timestamp,
    		@RequestHeader("Wechatpay-Serial") String serial,
    		@RequestBody String body, 
    		@PathVariable(name = "storeId") Integer storeId) {

        payServiceMap.get(Constant.PAY_TYPE.WX)
        		.refoundNotice(nonce, signature, signatureType, timestamp, serial, body, storeId);

        JSONObject result = new JSONObject();
        result.put("code", "SUCCESS");
        result.put("message", "成功");
        return result;
    }

}
